package org.zxw.service.impl;

import org.springframework.stereotype.Service;
import org.zxw.mapper.UserMapper;
import org.zxw.pojo.entity.User;
import org.zxw.service.LoginAndRegisterService;
import org.zxw.utils.JwtUtil;

@Service
public class LoginAndRegisterServiceImpl implements LoginAndRegisterService {

    private final JwtUtil jwtUtil;
    private final UserMapper userMapper;

    public LoginAndRegisterServiceImpl(JwtUtil jwtUtil, UserMapper userMapper) {
        this.jwtUtil = jwtUtil;
        this.userMapper = userMapper;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    public String login(String username, String password) {
        // 1. 验证用户名密码(MP)
        User user = userMapper.selectByUsernameAndPassword(username, password);
        if(user == null){
            return null;
        }

        // 2. 生成JWT
        return jwtUtil.generateToken(String.valueOf(user.getId()));
    }
}
